﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace SCSService
{
    public class ProductService
    {
        //Lấy mà thẻ theo id 
        public Product_Stock_Detail GetProductStockDetailById(int id)
        { 
            using(var data=new DataDataContext())
            {
                var type =(from psd in data.Product_Stock_Details
                            where psd.ProductStockDetailID==id
                               select psd).FirstOrDefault();
                return type;
            
            }
        }
        //Lấy về danh sách thẻ 
        public List<Product_Stock_Detail> GetListProduct_Stock_DetailByProductIDStatus(int? productID, int? Status, DateTime? EndDate,string Serial)
        {
            using (var data = new DataDataContext())
            {
                var result = (from p in data.Products
                              join ps in data.Product_Stocks on p.ProductID equals ps.ProductID into r0
                              from r1 in r0.DefaultIfEmpty()
                              join psd in data.Product_Stock_Details on r1.StockID equals psd.StockID
                              into r2
                              from r3 in r2
                              select new
                              {
                                  p.ProductID,
                                  r3.ProductStockDetailID,
                                  r3.Status,
                                  r3.EndDate,
                                  r3.Code,
                                  r3.Serial,
                                  p.ProductName,
                                  p.Product_Prices          
                              }).DefaultIfEmpty();




                if (productID != null)
                {
                    result = result.Where(m => m.ProductID == productID);
                }
                if (Status != null)
                {
                    result = result.Where(m => m.Status == Status);
                }
                if (EndDate != null)
                {
                    result = result.Where(m => m.EndDate < EndDate);
                }
                if (!string.IsNullOrEmpty(Serial))
                {
                    result = result.Where(m=>m.Serial.Contains(Serial));
                }
                var lstResultProduct_Stock_Detail = (from pst in result.ToList()
                                                     select new Product_Stock_Detail
                                                     {
                                                         ProductStockDetailID=pst.ProductStockDetailID,
                                                         Code=pst.Code,
                                                         Serial=pst.Serial,
                                                         EndDate=pst.EndDate,
                                                         Status=pst.Status
                                                     }).ToList();
                return lstResultProduct_Stock_Detail;

            }
        }
        //Lấy về số mã thẻ theo trạng thái thẻ 
        public int? GetCountProduct_Stock_DetailByProductIDStatus(int? productID, int? Status,DateTime? EndDate)
        { 
            using(var data=new DataDataContext())
            {
                var result = (from p in data.Products 
                              join ps in data.Product_Stocks on p.ProductID equals ps.ProductID into r0
                              from r1 in r0.DefaultIfEmpty()
                              join psd in data.Product_Stock_Details on r1.StockID equals psd.StockID
                              into r2 
                              from r3 in r2
                              select new { 
                              p.ProductID,
                              r3.Status,
                              r3.EndDate
                              }).DefaultIfEmpty();
                              

                              

                if (productID != null)
                {
                    result = result.Where(m => m.ProductID == productID);
                }
                if(Status!=null)
                {
                    result = result.Where(m=>m.Status==Status);
                }
                if (EndDate!=null)
                {
                    result = result.Where(m => m.EndDate < EndDate); 
                }
                return result.Count();

            }
        }
        //Lấy về tổng thẻ đã được thanh toán của từng lại thẻ
        public int? GetOrder_ItemByProductID_Status(int? productID, int? StatusInvoice)
        {
            using (var data = new DataDataContext())
            {
                var GetInvoice = (from ivoice in data.Invoices
                                  join order in data.Customer_Orders on ivoice.OrderID equals order.OrderID into io
                                  from r0 in io.DefaultIfEmpty()
                                  join r1 in data.Order_Items on r0.OrderID equals r1.OrderID
                                  into r2
                                  from r in r2.DefaultIfEmpty()
                                  where ivoice.Status == StatusInvoice
                                  select new {
                                      ivoice.Status,
                                      r.ProductID,
                                      r.Quantity
                                  });
                if (productID != null)
                {
                  GetInvoice=GetInvoice.Where(m=>m.ProductID==productID);
                }
                var countInvoice=(from cv in GetInvoice
                                  select cv.Quantity).Sum();

                return countInvoice;
            }
        }
        
        public void DeleteProductPriceByProductID(int productID)
        {
            using (var data = new DataDataContext())
            {
                var type = (from pr in data.Product_Prices
                            where pr.ProductID == productID
                            select pr);
                data.Product_Prices.DeleteAllOnSubmit(type);
                data.SubmitChanges();
            }

        }
        public void DeleteProduct_Stock_DetailByStockID(int? StockID)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Product_Stock_Details
                            where p.StockID == StockID
                            select p).ToList();
                data.Product_Stock_Details.DeleteAllOnSubmit(list);
                data.SubmitChanges();
            }
        }
        /// <summary>
        /// Get List TypeProduct by ParentId
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        /// 
        public List<Type_Product> GetListTypeProductByParent(int? parentID,int LanguagesID,int siteGuid,int status)
        {
            using (var data = new DataDataContext())
            {
                var item = (from p in data.Type_Products
                            where p.LanguagesID==LanguagesID && p.SiteGuid==siteGuid
                            //orderby p.SortOrder ascending
                            select p);
                if (parentID != null)
                {
                    item = item.Where(p => p.ParentTypeProductID==parentID);
                }
                else
                {
                    item = item.Where(p => p.ParentTypeProductID == null);
                }

                if (status != -1)
                {
                    item = item.Where(p => p.TypeProductStatus == status);
                }

                item = (from e in item
                        orderby e.SortOrder ascending
                        select e);

                return item.ToList();
            }
        }

        /// <summary>
        /// Get List TypeProduct by ParentId and Param is Null
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public List<Type_Product> GetListTypeProductByParentNotParam(int? parentID, int LanguagesID, int siteGuid, int status)
        {
            using (var data = new DataDataContext())
            {
                var item = (from p in data.Type_Products
                            where p.LanguagesID == LanguagesID && p.SiteGuid == siteGuid && p.TypeProductParam==null
                            //orderby p.SortOrder ascending
                            select p);
                if (parentID != null)
                {
                    item = item.Where(p => p.ParentTypeProductID == parentID);
                }
                else
                {
                    item = item.Where(p => p.ParentTypeProductID == null);
                }

                if (status != -1)
                {
                    item = item.Where(p => p.TypeProductStatus == status);
                }

                item = (from e in item
                        orderby e.SortOrder ascending
                        select e);

                return item.ToList();
            }
        }

        /// <summary>
        /// Get List TypeProduct By ParentID for Search 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <param name="order"></param>
        /// <param name="parentID"></param>
        /// <param name="LanguagesID"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>
        public List<Type_Product> GetListTypeProductByParentSearch(string name, int? status, int? order,string param, int? parentID, int LanguagesID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var item = (from p in data.Type_Products
                            where p.LanguagesID == LanguagesID && p.SiteGuid == siteGuid
                            select p);

                if (parentID != null)
                {
                    item = item.Where(p => p.ParentTypeProductID == parentID);
                }
                else
                {
                    item = item.Where(p => p.ParentTypeProductID == null);
                }

                if (name != string.Empty && name!=null)
                {
                    item = item.Where(p => p.TypeProductName.Contains(name));
                }

                if (status != null)
                {
                    item = item.Where(p => p.TypeProductStatus == status);
                }

                if (param != null && param != string.Empty)
                {
                    item = item.Where(p => p.TypeProductParam.Contains(param));
                }

                if (order != null)
                {
                    item = item.Where(p => p.SortOrder == order);
                }

                item = (from p in item
                        orderby p.SortOrder ascending
                        select p);

                return item.ToList();
            }
        }
        /// <summary>
        /// Get List TypeProduct Parent For TypeProduct Edit 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <param name="order"></param>
        /// <param name="parentID"></param>
        /// <param name="LanguagesID"></param>
        /// <param name="siteGuid"></param>
        /// <param name="idOwner"></param>
        /// <returns></returns>
        public List<Type_Product> GetListTypeProductByParentSearchForEdit(string name, int? status, int? order, int? parentID,string param, int LanguagesID, int siteGuid,int idOwner)
        {
            using (var data = new DataDataContext())
            {
                var item = (from p in data.Type_Products
                            where p.LanguagesID == LanguagesID && p.SiteGuid == siteGuid && p.TypeProductID !=idOwner
                            select p);

                if (parentID != null)
                {
                    item = item.Where(p => p.ParentTypeProductID == parentID);
                }
                else
                {
                    item = item.Where(p => p.ParentTypeProductID == null);
                }

                if (name != string.Empty && name != null)
                {
                    item = item.Where(p => p.TypeProductName.Contains(name));
                }

                if (status != null)
                {
                    item = item.Where(p => p.TypeProductStatus == status);
                }

                if (param != string.Empty && param != null)
                {
                    item = item.Where(p => p.TypeProductParam.Contains(param));
                }

                if (order != null)
                {
                    item = item.Where(p => p.SortOrder == order);
                }

                item = (from p in item
                        orderby p.SortOrder ascending
                        select p);

                return item.ToList();
            }
        }
        /// <summary>
        /// Get List TypeProduct By ParentID and Param
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <param name="order"></param>
        /// <param name="parentID"></param>
        /// <param name="param"></param>
        /// <param name="LanguagesID"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>
        public List<Type_Product> GetListTypeProductByParentIDAndParam(string name, int? status, int? order, int? parentID, string param, int LanguagesID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var item = (from p in data.Type_Products
                            where p.LanguagesID == LanguagesID && p.SiteGuid == siteGuid
                            select p);

                if (parentID != null)
                {
                    item = item.Where(p => p.ParentTypeProductID == parentID);
                }
                else
                {
                    item = item.Where(p => p.ParentTypeProductID == null);
                }

                if (name != string.Empty && name != null)
                {
                    item = item.Where(p => p.TypeProductName.Contains(name));
                }

                if (status != null)
                {
                    item = item.Where(p => p.TypeProductStatus == status);
                }

                if (param != string.Empty && param != null)
                {
                    item = item.Where(p => p.TypeProductParam.Contains(param));
                }

                if (order != null)
                {
                    item = item.Where(p => p.SortOrder == order);
                }

                item = (from p in item
                        orderby p.SortOrder ascending
                        select p);

                return item.ToList();
            }
        }

        /// <summary>
        /// Get TypeProduct By ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Type_Product GetTypeProduct(int id)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Type_Products
                            where p.TypeProductID == id
                            select p).FirstOrDefault();
                return type;
            }
        }

        /// <summary>
        /// Get TypeProductById and siteGuid
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Type_Product GetTypeProductBysiteGuid(int id,int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Type_Products
                            where p.TypeProductID == id && p.SiteGuid==siteGuid && p.TypeProductStatus==1
                            select p).FirstOrDefault();
                return type;
            }
        }

        /// <summary>
        /// Đệ quy list ID danh mục loại sản phẩm
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public StringBuilder GetListTypeProductByID(ref StringBuilder sb ,int id,int LanguagesID,int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Type_Products
                            where p.LanguagesID == LanguagesID && p.SiteGuid == siteGuid && p.ParentTypeProductID==id
                            select p.TypeProductID).ToList();
                foreach (var item in type)
                {
                    sb.Append(item+",");
                    if (type.Count() > 0)
                    {
                        GetListTypeProductByID(ref sb, item,LanguagesID,siteGuid);
                    }
                }

                return sb;
            }
        }


        /// <summary>
        /// Get List Product By TypeProduct
        /// </summary>
        /// <param name="typeID"></param>
        /// <returns></returns>
        public List<Product> GetListProductByTypeProduct(int? typeID, int LanguagesID, int siteGuid,int? status)
        {
            using (var data = new DataDataContext())
            {
                List<Product> listProduct=new List<Product>();

                if (typeID != null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb = GetListTypeProductByID(ref sb, (int)typeID,LanguagesID,siteGuid);
                    var arrId = sb.ToString();
                    listProduct = (from p in data.Products
                                   join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                   from e0 in r0.DefaultIfEmpty()
                                   join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                   from e1 in r1.DefaultIfEmpty()
                                   where e1.LanguagesID==LanguagesID && p.Status==1 && e1.SiteGuid==siteGuid && (e1.TypeProductID==typeID || arrId.Substring(0,arrId.Length).Split(',').Contains(e1.TypeProductID.ToString()))
                                   select p).ToList();
                }
                else
                {
                    listProduct = (from p in data.Products
                                   join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                   from e0 in r0.DefaultIfEmpty()
                                   join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                   from e1 in r1.DefaultIfEmpty()
                                   where e1.LanguagesID==LanguagesID && p.Status==1 && e1.SiteGuid==siteGuid
                                   select p).ToList();
                }

                if (status != null)
                {
                    listProduct = listProduct.Where(p => p.Status == (byte)status).ToList();
                }

                return listProduct.Distinct().ToList();
            }
        }

        /// <summary>
        /// Get List Product for Filter
        /// </summary>
        /// <param name="typeID"></param>
        /// <param name="LanguagesID"></param>
        /// <param name="siteGuid"></param>
        /// <param name="status"></param>
        /// <param name="listFilter"></param>
        /// <returns></returns>
        public List<Product> GetListProductByTypeProductFilter(int? typeID, int LanguagesID, int siteGuid, int? status, List<Type_Property> listFilter)
        {
            using (var data = new DataDataContext())
            {
                List<Product> listProduct = new List<Product>();

                if (typeID != null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb = GetListTypeProductByID(ref sb, (int)typeID, LanguagesID, siteGuid);
                    var arrId = sb.ToString();

                    if (listFilter.Count > 0)
                    {
                        var listTypePropertiesID = listFilter.Select(p=>p.TypePropertiesID).ToList();

                        listProduct = (from p in data.Products
                                       join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                       from e0 in r0.DefaultIfEmpty()
                                       join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                       from e1 in r1.DefaultIfEmpty()
                                       join ptp in data.Product_Type_Properties on p.ProductID equals ptp.ProductID //into r2
                                       //from e2 in r2.DefaultIfEmpty()
                                       join tp in data.Type_Properties on ptp.TypePropertiesID equals tp.TypePropertiesID into r3
                                       from e3 in r3.DefaultIfEmpty()
                                       where e1.LanguagesID == LanguagesID && p.Status == 1 && e1.SiteGuid == siteGuid && (e1.TypeProductID == typeID || arrId.Substring(0, arrId.Length).Split(',').Contains(e1.TypeProductID.ToString()))
                                       && listTypePropertiesID.Contains(ptp.TypePropertiesID??0)
                                       select p).ToList();
                    }
                    else
                    {
                        listProduct = (from p in data.Products
                                       join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                       from e0 in r0.DefaultIfEmpty()
                                       join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                       from e1 in r1.DefaultIfEmpty()
                                       where e1.LanguagesID == LanguagesID && p.Status == 1 && e1.SiteGuid == siteGuid && (e1.TypeProductID == typeID || arrId.Substring(0, arrId.Length).Split(',').Contains(e1.TypeProductID.ToString()))
                                       select p).ToList();
                    }
                }
                else
                {
                    if (listFilter.Count > 0)
                    {
                        var listTypePropertiesID = listFilter.Select(p => p.TypePropertiesID).ToList();

                        listProduct = (from p in data.Products
                                       join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                       from e0 in r0.DefaultIfEmpty()
                                       join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                       from e1 in r1.DefaultIfEmpty()
                                       join ptp in data.Product_Type_Properties on p.ProductID equals ptp.ProductID //into r2
                                       //from e2 in r2.DefaultIfEmpty()
                                       join tp in data.Type_Properties on ptp.TypePropertiesID equals tp.TypePropertiesID into r3
                                       from e3 in r3.DefaultIfEmpty()
                                       where e1.LanguagesID == LanguagesID && p.Status == 1 && e1.SiteGuid == siteGuid && listTypePropertiesID.Contains(ptp.TypePropertiesID ?? 0)
                                       select p).ToList();
                    }
                    else
                    {
                        listProduct = (from p in data.Products
                                       join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                       from e0 in r0.DefaultIfEmpty()
                                       join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                       from e1 in r1.DefaultIfEmpty()
                                       where e1.LanguagesID == LanguagesID && p.Status == 1 && e1.SiteGuid == siteGuid
                                       select p).ToList();
                    }
                    
                }

                if (status != null)
                {
                    listProduct = listProduct.Where(p => p.Status == (byte)status).ToList();
                }

                return listProduct.Distinct().ToList();
            }
        }
        /// <summary>
        /// Get Top 10 product Last (order desc)
        /// </summary>
        /// <param name="LanguagesID"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>
        public List<Product> GetListTopTenProduct(int LanguagesID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                List<Product> listProduct = new List<Product>();

                
                  listProduct = (from p in data.Products
                                   join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                   from e0 in r0.DefaultIfEmpty()
                                   join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                   from e1 in r1.DefaultIfEmpty()
                                   where e1.LanguagesID == LanguagesID && e1.SiteGuid == siteGuid && p.Status==1
                                   orderby p.ProductID descending
                                   select p).Take(10).ToList();
                
                return listProduct;
            }
        }
        /// <summary>
        /// Get Product By Type Product or Not (have search)
        /// </summary>
        /// <param name="ProductNameSearch"></param>
        /// <param name="ProductCodeSearch"></param>
        /// <param name="typeID"></param>
        /// <param name="LanguagesID"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>
        public List<Product> GetListProductByTypeProductSearch(string ProductNameSearch, string ProductCodeSearch, int? typeID, int LanguagesID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                //List<Product> listProduct = new List<Product>();

                if (typeID != null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb = GetListTypeProductByID(ref sb, (int)typeID, LanguagesID, siteGuid);
                    var arrId = sb.ToString();
                    var listProduct = (from p in data.Products
                                   join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                   from e0 in r0.DefaultIfEmpty()
                                   join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                   from e1 in r1.DefaultIfEmpty()
                                   where e1.LanguagesID == LanguagesID && e1.SiteGuid==siteGuid && e1.SiteGuid==siteGuid && (e1.TypeProductID == typeID || arrId.Substring(0, arrId.Length).Split(',').Contains(e1.TypeProductID.ToString()))
                                   select p).Distinct();

                    if (ProductNameSearch != string.Empty && ProductNameSearch != null)
                    {
                        listProduct = listProduct.Where(p => p.ProductName.Contains(ProductNameSearch));
                    }

                    if (ProductCodeSearch != string.Empty && ProductCodeSearch != null)
                    {
                        listProduct = listProduct.Where(p => p.ProductCode.Contains(ProductCodeSearch));
                    }

                    return listProduct.ToList();
                }
                else
                {
                    var listProduct = (from p in data.Products
                                   join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                   from e0 in r0.DefaultIfEmpty()
                                   join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                   from e1 in r1.DefaultIfEmpty()
                                   where e1.LanguagesID == LanguagesID && e1.SiteGuid == siteGuid
                                   select p).Distinct();

                    if (ProductNameSearch != string.Empty && ProductNameSearch != null)
                    {
                        listProduct = listProduct.Where(p => p.ProductName.Contains(ProductNameSearch));
                    }

                    if (ProductCodeSearch != string.Empty && ProductCodeSearch != null)
                    {
                        listProduct = listProduct.Where(p => p.ProductCode.Contains(ProductCodeSearch));
                    }

                    return listProduct.ToList();
                }

                
            }
        }

        /// <summary>
        /// Get count Type Product in website
        /// </summary>
        /// <param name="LanguagesID"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>

        public int GetCountTypeProduct(int LanguagesID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var item = (from p in data.Type_Products
                            where p.LanguagesID == LanguagesID && p.SiteGuid == siteGuid
                            select p).Count();

               
                return item;
            }
        }

        /// <summary>
        /// Update Status of Type Product
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newStatus"></param>
        public void UpdateStatusTypeProduct(int? id, int? newStatus)
        {
            using (var data = new DataDataContext())
            {
                if (id != null && newStatus != null)
                {
                    var type = (from p in data.Type_Products
                                   where p.TypeProductID == id
                                   select p).FirstOrDefault();
                    if (type != null)
                    {
                        if (type.TypeProductStatus != newStatus || type.TypeProductStatus == null)
                            type.TypeProductStatus = (byte)newStatus;

                        data.SubmitChanges();
                    }
                }

            }
        }


        /// <summary>
        /// Update Sort Order of Type Product
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newSort"></param>
        public void UpdateSortOrder(int? id, int? newSort)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Type_Products
                            where p.TypeProductID == id
                            select p).FirstOrDefault();
                if (type != null && newSort!=null)
                {
                    if (type.SortOrder != newSort || type.SortOrder == null)
                        type.SortOrder = newSort;

                    data.SubmitChanges();
                }
            }
        }


        /// <summary>
        /// Get Count Product in 1 Type Product
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public int GetCountProductInTypeProduct(int typeId)
        {
            using (var data = new DataDataContext())
            {
                var ProMana = (from p in data.Type_Product_Manages
                               where p.TypeProductID == typeId
                               select p);
                return ProMana.Count();
            }
        }

        /// <summary>
        /// Delete TypeProduc for Type Product have not Product
        /// </summary>
        /// <param name="typeId"></param>
        public void DeleteTypeProduct(int typeId)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Type_Products
                            where p.TypeProductID == typeId
                            select p).FirstOrDefault();
                data.Type_Products.DeleteOnSubmit(type);
                data.SubmitChanges();
            }
        }


        /// <summary>
        /// Get Product by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product GetProduct(int id)
        {
            using (var data = new DataDataContext())
            {
                var product = (from p in data.Products
                               where p.ProductID == id
                               select p).FirstOrDefault();
                return product;
            }
        }
        /// <summary>
        /// Get Product By Id and SiteGuid
        /// </summary>
        /// <param name="id"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>
        public Product GetProductBySiteGuid(int id,int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var product = (from p in data.Products
                               join tpm in data.Type_Product_Manages on p.ProductID equals tpm.ProductID into r0
                               from e0 in r0.DefaultIfEmpty()
                               join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                               from e1 in r1.DefaultIfEmpty()
                               where p.ProductID == id && e1.SiteGuid==siteGuid && p.Status==1
                               select p).FirstOrDefault();
                return product;
            }
        }
        /// <summary>
        /// Get Top 1 price of product desc
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product_Price GetProductPriceByProductID(int id,int? languageID,int? currencyID)
        {
            using (var data = new DataDataContext())
            {
                var proPrice = (from p in data.Product_Prices
                                where (p.ProductID == id) && (p.StartDate <=DateTime.Now) && p.Satus==1                                
                                select p);

                if (currencyID != null)
                {
                    proPrice = proPrice.Where(p => p.CurrencyID == currencyID);
                }

                if (languageID != null)
                {
                    proPrice = proPrice.Where(p => p.LanguagesID == languageID);
                }
                else
                    proPrice = proPrice.Where(p => p.LanguagesID == 1);         //1 la vi-VN


                proPrice = (from p in proPrice
                            orderby p.StartDate descending
                            select p);

                if (proPrice.Count()>0)
                {
                    if (proPrice.FirstOrDefault().EndDate != null)
                    {
                        proPrice = (from p in proPrice
                                    where p.EndDate >= DateTime.Now
                                    select p);
                    }
                }
                
                return proPrice.FirstOrDefault();
            }
        }
        /// <summary>
        /// Get Discount by ProductID and LanguageID
        /// </summary>
        /// <param name="id"></param>
        /// <param name="languageID"></param>
        /// <returns></returns>
        public Discount GetDiscountByProductID(int id, int? languageID)
        {
            using (var data = new DataDataContext())
            {
                var discount = (from p in data.Products
                                join dpm in data.Discount_Product_Managers on p.ProductID equals dpm.ProductID into r0
                                from e0 in r0.DefaultIfEmpty()
                                join d in data.Discounts on e0.DiscountsID equals d.DiscountsID into r1
                                from e1 in r1.DefaultIfEmpty()
                                where (p.ProductID == id) && (e1.StartDate <= DateTime.Now) && e1.Status == 1
                                select e1);

               
                if (languageID != null)
                {
                    discount = discount.Where(p => p.LanguagesID == languageID);
                }
                else
                    discount = discount.Where(p => p.LanguagesID == 1);         //1 la vi-VN


                discount = (from p in discount
                            orderby p.StartDate descending
                            select p);

                if (discount.Count() > 0)
                {
                    if (discount.FirstOrDefault().EndDate != null)
                    {
                        discount = (from p in discount
                                    where p.EndDate >= DateTime.Now
                                    select p);
                    }
                }

                return discount.FirstOrDefault();
            }
        }

        /// <summary>
        /// Get List product price by productID
        /// </summary>
        /// <param name="id"></param>
        /// <param name="languageID"></param>
        /// <param name="currencyID"></param>
        /// <returns></returns>
        public List<Product_Price> GetListProductPriceByProductID(int id, int? languageID, int? currencyID)
        {
            using (var data = new DataDataContext())
            {
                var proPrice = (from p in data.Product_Prices
                                where (p.ProductID == id) && (p.StartDate <= DateTime.Now) && p.Satus == 1
                                select p);

                if (currencyID != null)
                {
                    proPrice = proPrice.Where(p => p.CurrencyID == currencyID);
                }

                if (languageID != null)
                {
                    proPrice = proPrice.Where(p => p.LanguagesID == languageID);
                }
                else
                    proPrice = proPrice.Where(p => p.LanguagesID == 1);         //1 la vi-VN


                proPrice = (from p in proPrice
                            orderby p.StartDate descending
                            select p);

                ////if (proPrice.Count() > 0)
                ////{
                ////    if (proPrice.FirstOrDefault().EndDate != null)
                ////    {
                ////        proPrice = (from p in proPrice
                ////                    where p.EndDate >= DateTime.Now
                ////                    select p);
                ////    }
                ////}

                var proPrice1 = proPrice.Where(p => p.EndDate != null && p.EndDate >= DateTime.Now);
                var proPrice2 = proPrice.Where(p => p.EndDate == null);

                var returns = proPrice1.Union(proPrice2);

                return returns.ToList();
            }
        }

        /// <summary>
        /// Get List Product Price by ProductID
        /// </summary>
        /// <param name="id"></param>
        /// <param name="languageID"></param>
        /// <returns></returns>
        public List<Product_Price> GetListProductPriceByProductID(int id, int? languageID, DateTime? StartDateSearch, DateTime? EndDateSearch, float? ValueSearch, int? StatusSearch)
        {
            using (var data = new DataDataContext())
            {
                var proPrice = (from p in data.Product_Prices
                                where (p.ProductID == id)// && (p.StartDate <= DateTime.Now)
                                select p);

                if (languageID != null)
                {
                    proPrice = proPrice.Where(p => p.LanguagesID == languageID);
                }
                else
                    proPrice = proPrice.Where(p => p.LanguagesID == 1);         //1 la vi-VN


                if (StartDateSearch != null && EndDateSearch==null)
                {
                    proPrice = proPrice.Where(p => p.StartDate !=null && p.StartDate>=StartDateSearch);
                }

                if (StartDateSearch==null && EndDateSearch != null)
                {
                    proPrice = proPrice.Where(p => p.EndDate !=null && p.EndDate<=EndDateSearch);
                }

                if (StartDateSearch != null && EndDateSearch != null)
                {
                    proPrice = proPrice.Where(p => p.StartDate != null && p.StartDate >= StartDateSearch && p.StartDate <= EndDateSearch); 
                }

                if (ValueSearch != null)
                {
                    proPrice = proPrice.Where(p => p.PriceValue == ValueSearch);
                }

                if (StatusSearch != null)
                {
                    proPrice = proPrice.Where(p => p.Satus == StatusSearch);
                }

                proPrice = (from p in proPrice
                            orderby p.StartDate descending
                            select p);

                //if (proPrice.Count() > 0)
                //{
                //    if (proPrice.FirstOrDefault().EndDate != null)
                //    {
                //        proPrice = (from p in proPrice
                //                    where p.EndDate >= DateTime.Now
                //                    select p);
                //    }
                //}

                return proPrice.ToList();
            }
        } 

        /// <summary>
        /// Update Status of Product
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newStatus"></param>
        public void UpdateStatusProduct(int? id,int? newStatus)
        {
            using (var data = new DataDataContext())
            {
                if (id != null && newStatus != null)
                {
                    var product = (from p in data.Products
                                   where p.ProductID == id
                                   select p).FirstOrDefault();
                    if (product != null)
                    {
                        if (product.Status != newStatus || product.Status == null)
                            product.Status = (byte)newStatus;

                        data.SubmitChanges();
                    }
                }
                
            }
        }

        /// <summary>
        /// Get No product in stock
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public long GetProductStockByProductID(int id,int status)
        {
            using (var data = new DataDataContext())
            {
                var proSum = (from p in data.Product_Stocks
                              where p.ProductID == id && p.Quantity != null
                              select p);
                if (status != -1)
                {
                    proSum = proSum.Where(p => p.Status == status);
                }
                
                var proStoc = (from p in proSum
                               select p.Quantity).Sum();
                if (proStoc != null)
                {
                    return (long)proStoc;
                }
                else
                    return 0;
                
            }
        }

        /// <summary>
        /// Get No product in stock
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public long GetProductStockDetailByProductID(int id, int status)
        {
            using (var data = new DataDataContext())
            {
                var proSum = (from p in data.Product_Stocks
                              join sd in data.Product_Stock_Details on p.StockID equals sd.StockID
                              where p.ProductID == id && p.Quantity != null && sd.Status==1
                              select p);
                if (status != -1)
                {
                    proSum = proSum.Where(p => p.Status == status);
                }

                //var proStoc = (from p in proSum
                //               select p.Quantity).Sum();
                if (proSum != null)
                {
                    return (long)proSum.Count();
                }
                else
                    return 0;

            }
        }

        /// <summary>
        /// Get stock of Product
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public Product_Stock GetProductInStockByProductID(int id, int status,int statusStockDetail)
        {
            using (var data = new DataDataContext())
            {
                var stock = (from p in data.Product_Stocks
                             join sd in data.Product_Stock_Details on p.StockID equals sd.StockID
                             where p.ProductID == id && p.Status == status && p.Quantity != null && sd.Status == statusStockDetail
                             select p).FirstOrDefault();

                return stock;
                
            }
        }
        /// <summary>
        /// Get List Product_Stock by ProductID
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <param name="statusStockDetail"></param>
        /// <returns></returns>
        //public List<Product_Stock> GetListProductInStockByProductID(int id, int status, int statusStockDetail)
        //{
        //    using (var data = new DataDataContext())
        //    {
        //        var list = (from p in data.Product_Stocks
        //                     join sd in data.Product_Stock_Details on p.StockID equals sd.StockID
        //                     where p.ProductID == id && p.Status == status && p.Quantity != null && sd.Status == statusStockDetail
        //                     select p).ToList();

        //        return list;

        //    }
        //}

        public List<Product_Stock_Detail> GetListNoProductStockDetailByProductID(int id, int No, int status, int statusStockDetail)
        {
            using (var data = new DataDataContext())
            {
                var listDetail = (from p in data.Product_Stock_Details
                                  join ps in data.Product_Stocks on p.StockID equals ps.StockID
                                  where ps.ProductID==id && ps.Status == status && ps.Quantity!=null && p.Status==statusStockDetail
                                  select p);
                return listDetail.Take(No).ToList();
            }
        }

        public List<Product_Stock_Detail> GetListProductStockDetailByProductID(int id, int status, int statusStockDetail)
        {
            using (var data = new DataDataContext())
            {
                var stock = (from p in data.Product_Stocks
                             join sd in data.Product_Stock_Details on p.StockID equals sd.StockID
                             where p.ProductID == id && p.Status == status && p.Quantity != null && sd.Status == statusStockDetail
                             select sd);

                return stock.ToList();

            }
        }

        public List<Product_Stock_Detail> GetListNoProductStockDetail(int No, int stockID)
        {
            using (var data = new DataDataContext())
            {
                var listDetail = (from p in data.Product_Stock_Details
                                  where p.StockID == stockID && p.Status==1
                                  select p);
                return listDetail.Take(No).ToList();
            }
        }
        /// <summary>
        /// Get List Product stock`
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<Product_Stock> GetListProductStockByProductID(int id, int status)
        {
            using (var data = new DataDataContext())
            {
                var proSum = (from p in data.Product_Stocks
                              where p.ProductID == id && p.Quantity != null
                              select p);
                if (status != -1)
                {
                    proSum = proSum.Where(p => p.Status == status);
                }


                return proSum.ToList();
            }
        }

        /// <summary>
        /// Create Type Product
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public int CreateTypeProduct(Type_Product type)
        {
            using (var data = new DataDataContext())
            {
                data.Type_Products.InsertOnSubmit(type);
                data.SubmitChanges();

                return type.TypeProductID;
            }
        }

        /// <summary>
        /// Update TypeProduct
        /// </summary>
        /// <param name="type"></param>
        public void UpdateTypeProduct(Type_Product type)
        {
            using (var data = new DataDataContext())
            {
                var typeProduct = (from p in data.Type_Products
                                   where p.TypeProductID == type.TypeProductID
                                   select p).SingleOrDefault();
                if (type.TypeProductName != null)
                {
                    typeProduct.TypeProductName = type.TypeProductName;
                }

                if (type.TypeProductAbstract != null)
                {
                    typeProduct.TypeProductAbstract = type.TypeProductAbstract;
                }

                if (type.TypeProductDes != null)
                {
                    typeProduct.TypeProductDes = type.TypeProductDes;
                }

                if (type.TypeProductImage != null)
                {
                    typeProduct.TypeProductImage = type.TypeProductImage;
                }

                if (type.TypeProductStatus != null)
                {
                    typeProduct.TypeProductStatus = type.TypeProductStatus;
                }

                if (type.LanguagesID != null)
                {
                    typeProduct.LanguagesID = type.LanguagesID;
                }

                if (type.ParentTypeProductID != null)
                {
                    typeProduct.ParentTypeProductID = type.ParentTypeProductID;
                }
                if(!string.IsNullOrEmpty(type.TypeProductParam))
                {
                    typeProduct.TypeProductParam = type.TypeProductParam;
                }

                

                data.SubmitChanges();
            }
        }

        public List<Type_Property> GetListTypePropertiesByProductID(int productID,int languageId,int status)
        {
            using (var data = new DataDataContext())
            {
                var typeProperties = (from tp in data.Type_Properties
                                      join ptp in data.Product_Type_Properties on tp.TypePropertiesID equals ptp.TypePropertiesID into r0
                                      from e0 in r0.DefaultIfEmpty()
                                      join p in data.Products on e0.ProductID equals p.ProductID into r1
                                      from e1 in r1.DefaultIfEmpty()
                                      where e1.ProductID == productID && tp.LanguagesID==languageId
                                      select tp);
                if (status != -1)
                {
                    typeProperties = typeProperties.Where(p => p.Status == status);
                }
                return typeProperties.ToList();
            }
        }


        public List<Product_Property> GetListProductProperties(int? typeid,int? proId)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Product_Properties
                            join ptp in data.Product_Type_Properties on p.ProductTypePropertiesID equals ptp.ProductTypePropertiesID
                            where ptp.TypePropertiesID == typeid && ptp.ProductID==proId
                            select p).ToList();
                if (typeid != null)
                    return list;
                else
                    return null;
            }
        }

        public Product_Type_Property GetProductTypePropertiesByProductIDandTypePropertiesID(int productId, int typePropertiesId)
        {
            using (var data = new DataDataContext())
            {
                var result = (from p in data.Product_Type_Properties
                              where p.ProductID == productId && p.TypePropertiesID == typePropertiesId
                              select p).FirstOrDefault();
                return result;
            }
        }


        public Type_Property GetTypePropertiesByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Type_Properties
                            where p.TypePropertiesID == id
                            select p).FirstOrDefault();
                return type;
            }
        }

        /// <summary>
        /// Create Product
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public int CreateProduc(Product product)
        {

            using (var data = new DataDataContext())
            {
                data.Products.InsertOnSubmit(product);
                data.SubmitChanges();

                return product.ProductID;
            }
        }

        /// <summary>
        /// Create Type Product Manage
        /// </summary>
        /// <param name="manage"></param>
        /// <returns></returns>
        public int CreateManageProduct(Type_Product_Manage manage)
        {
            using (var data = new DataDataContext())
            {
                data.Type_Product_Manages.InsertOnSubmit(manage);
                data.SubmitChanges();

                return manage.ManageID;
            }
        }

        /// <summary>
        /// Get List Id of Type Product by product id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<int> GetListTypeProductIdManageByProductID(int id)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Type_Product_Manages
                            where p.ProductID == id
                            select (int)p.TypeProductID).ToList();
                return list;
            }
        }


        /// <summary>
        /// Get list ProductManage by ProductID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Type_Product_Manage> GetListProductManageByProductID(int id)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Type_Product_Manages
                            where p.ProductID == id
                            select p).ToList();
                return list;
            }
        }

        /// <summary>
        /// Delete by ManageID
        /// </summary>
        /// <param name="manageID"></param>
        public void DeleteProductManage(int manageID)
        {
            using (var data = new DataDataContext())
            {
                var mana = (from p in data.Type_Product_Manages
                            where p.ManageID == manageID
                            select p).FirstOrDefault();
                data.Type_Product_Manages.DeleteOnSubmit(mana);
                data.SubmitChanges();
            }
        }
        /// <summary>
        /// Delete Product
        /// </summary>
        /// <param name="id"></param>
        public void DeleteProduct(int id)
        {
            using (var data = new DataDataContext())
            {
                var pro = (from p in data.Products
                            where p.ProductID == id
                            select p).FirstOrDefault();
                data.Products.DeleteOnSubmit(pro);
                data.SubmitChanges();
            }
        }
        /// <summary>
        /// Update for Product
        /// </summary>
        /// <param name="pro"></param>
        public void UpdateProduct(Product pro)
        {
            using (var data = new DataDataContext())
            {
                var product = (from p in data.Products
                               where p.ProductID == pro.ProductID
                               select p).FirstOrDefault();
                product.ProductName = pro.ProductName;
                product.ProductDes = pro.ProductDes;
                product.ProductCode = pro.ProductCode;
                product.Images = pro.Images;
                product.ModifyDate = pro.ModifyDate;
                product.ModifyUser = pro.ModifyUser;
                product.CreateDate = pro.CreateDate;
                product.CreateUser = pro.CreateUser;
                product.Features = pro.Features;
                product.Status = pro.Status;

                data.SubmitChanges();
            }
        }

        /// <summary>
        /// Update Status Price
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newStatus"></param>
        public void UpdateStatusPrice(int? id, int? newStatus)
        {
            using (var data = new DataDataContext())
            {
                if (id != null && newStatus != null)
                {
                    var price = (from p in data.Product_Prices
                                   where p.PriceID == id
                                   select p).FirstOrDefault();
                    if (price != null)
                    {
                        if (price.Satus != newStatus || price.Satus == null)
                            price.Satus = (byte)newStatus;

                        data.SubmitChanges();
                    }
                }

            }
        }


        /// <summary>
        /// Create discount
        /// </summary>
        /// <param name="dis"></param>
        /// <returns></returns>
        public int CreateDiscount(Discount dis)
        {
            using (var data = new DataDataContext())
            {
                data.Discounts.InsertOnSubmit(dis);
                data.SubmitChanges();

                return dis.DiscountsID;
            }
        }

        /// <summary>
        /// Get Discount with id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Discount GetDiscountByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var discount = (from p in data.Discounts
                                where p.DiscountsID == id
                                select p).FirstOrDefault();
                return discount;
            }
        }


        /// <summary>
        /// Create Discount Manager
        /// </summary>
        /// <param name="disMana"></param>
        public void CreateDiscountManage(Discount_Product_Manager disMana)
        {
            using (var data = new DataDataContext())
            {
                data.Discount_Product_Managers.InsertOnSubmit(disMana);
                data.SubmitChanges();
            }
        }


        /// <summary>
        /// Get List ProductId in Discount_Product_Manage By DiscountID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<int> GetListProducIDManageByDiscountID(int id)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Discount_Product_Managers
                            where p.DiscountsID == id
                            select (int)p.ProductID).ToList();
                return list;
            }
        }

        /// <summary>
        /// Get List TypeProductID By list ProductID
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<int> GetListTypeProductByListProductID(List<int> list)
        {
            using (var data = new DataDataContext())
            {
                var listReturn = (from p in data.Type_Product_Manages
                                  where p.ProductID != null
                                  select p);

                listReturn = listReturn.Where(p => list.Contains((int)p.ProductID));

                
                //listReturn=listReturn.Where(p=>list.ToString().Contains(p.ProductID.ToString()));

                var result = (from p in listReturn
                              select (int)p.TypeProductID);

                return result.ToList();
            }
        }

        /// <summary>
        /// Update Content of Discount
        /// </summary>
        /// <param name="dis"></param>
        public void UpdateDiscount(Discount dis)
        {
            using (var data = new DataDataContext())
            {
                var discount = (from p in data.Discounts
                                where p.DiscountsID == dis.DiscountsID
                                select p).FirstOrDefault();

                discount.TypeDiscounts = dis.TypeDiscounts;
                discount.StartDate = dis.StartDate;
                discount.EndDate = dis.EndDate;
                discount.DiscountPercent = dis.DiscountPercent;
                discount.DiscountValue = dis.DiscountValue;
                discount.Description = dis.Description;

                data.SubmitChanges();
            }
        }

        /// <summary>
        /// Get List Product in Discount_Product_Manager
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Discount_Product_Manager> GetListProductManageByDiscountID(int id)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Discount_Product_Managers
                            where p.ProductID == id
                            select p).ToList();
                return list;
            }
        }

        /// <summary>
        /// Delete Discount_Product_Manage
        /// </summary>
        /// <param name="disId"></param>
        public void DeleteProductManageDiscount(int disId,int proId)
        {
            using (var data = new DataDataContext())
            {
                var mana = (from p in data.Discount_Product_Managers
                            where p.DiscountsID == disId && p.ProductID==proId
                            select p).FirstOrDefault();
                data.Discount_Product_Managers.DeleteOnSubmit(mana);
                data.SubmitChanges();
            }
        }

        /// <summary>
        /// Get List Discount
        /// </summary>
        /// <param name="id"></param>
        /// <param name="languageID"></param>
        /// <param name="StartDateSearch"></param>
        /// <param name="EndDateSearch"></param>
        /// <param name="ValueSearch"></param>
        /// <param name="StatusSearch"></param>
        /// <param name="TypeDiscounts"></param>
        /// <returns></returns>
        public List<Discount> GetListDiscountManagerByProductID(int? id, int? languageID, DateTime? StartDateSearch, DateTime? EndDateSearch, float? ValueSearch, int? StatusSearch, int? TypeDiscounts)
        {
            using (var data = new DataDataContext())
            {
                IQueryable<Discount> dis = null;
                if (id != null)
                {
                    dis = (from p in data.Discounts
                           join pdm in data.Discount_Product_Managers on p.DiscountsID equals pdm.ProductID into r0
                           from e0 in r0.DefaultIfEmpty()
                           join pr in data.Products on e0.ProductID equals pr.ProductID into r1
                           from e1 in r1.DefaultIfEmpty()
                           where e1.ProductID==id
                           select p);
                }
                else
                {
                    dis = (from p in data.Discounts
                           join pdm in data.Discount_Product_Managers on p.DiscountsID equals pdm.ProductID into r0
                           from e0 in r0.DefaultIfEmpty()
                           join pr in data.Products on e0.ProductID equals pr.ProductID into r1
                           from e1 in r1.DefaultIfEmpty()
                           select p);
                }

                if (languageID != null)
                {
                    dis = dis.Where(p => p.LanguagesID == languageID);
                }
                else
                    dis = dis.Where(p => p.LanguagesID == 1);         //1 la vi-VN


                if (StartDateSearch != null && EndDateSearch == null)
                {
                    dis = dis.Where(p => p.StartDate != null && p.StartDate >= StartDateSearch);
                }

                if (StartDateSearch == null && EndDateSearch != null)
                {
                    dis = dis.Where(p => p.EndDate != null && p.EndDate <= EndDateSearch);
                }

                if (StartDateSearch != null && EndDateSearch != null)
                {
                    dis = dis.Where(p => p.StartDate != null && p.StartDate >= StartDateSearch && p.StartDate <= EndDateSearch);
                }

                if (TypeDiscounts != -1)
                {
                    if (TypeDiscounts == 1)
                    {
                        //Percent
                        dis = dis.Where(p => p.DiscountPercent == ValueSearch);
                    }
                    else if (TypeDiscounts == 0)
                    { 
                        //Value
                        dis = dis.Where(p => p.DiscountValue == ValueSearch);
                    }
                    
                }

                if (StatusSearch != null)
                {
                    dis = dis.Where(p => p.Status == StatusSearch);
                }

                dis = (from p in dis
                            orderby p.StartDate descending
                            select p);

                //if (dis.Count() > 0)
                //{
                //    if (dis.FirstOrDefault().EndDate != null)
                //    {
                //        proPrice = (from p in proPrice
                //                    where p.EndDate >= DateTime.Now
                //                    select p);
                //    }
                //}

                return dis.ToList();
            }
        }

        /// <summary>
        /// Get List Currency
        /// </summary>
        /// <returns></returns>
        public List<System_Currency> GetListCurrency()
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.System_Currencies
                            select p).ToList();
                return list;
            }
        }

        /// <summary>
        /// Get currency By ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public System_Currency GetCurrencyByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var currency = (from p in data.System_Currencies
                                where p.CurrencyID==id
                                select p).FirstOrDefault();
                return currency;
            }
            
        }

        /// <summary>
        /// Creat Price of Product
        /// </summary>
        /// <param name="pri"></param>
        /// <returns></returns>
        public int CreatePrice(Product_Price pri)
        {
            using (var data = new DataDataContext())
            {
                data.Product_Prices.InsertOnSubmit(pri);
                data.SubmitChanges();
                return pri.PriceID;
            }
        }

        /// <summary>
        /// Get Price by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product_Price GetProductPriceByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var price = (from p in data.Product_Prices
                             where p.PriceID == id
                             select p).FirstOrDefault();
                return price;
            }
        }

        /// <summary>
        /// Update Product Price
        /// </summary>
        /// <param name="pri"></param>
        public void UpdateProductPrice(Product_Price pri)
        {
            using (var data = new DataDataContext())
            {
                var price = (from p in data.Product_Prices
                             where p.PriceID == pri.PriceID
                             select p).FirstOrDefault();

                price.PriceValue = pri.PriceValue;
                //price.StartDate = pri.StartDate;
                price.EndDate = pri.EndDate;
                price.Notes = pri.Notes;
                price.ProductID = pri.ProductID;
                price.LanguagesID = pri.LanguagesID;
                price.CurrencyID = pri.CurrencyID;
                price.Satus = pri.Satus;

                data.SubmitChanges();
            }
        }

        /// <summary>
        /// Delete Price
        /// </summary>
        /// <param name="priceID"></param>
        public void DeletePrice(int priceID)
        {
            using (var data = new DataDataContext())
            {
                var price = (from p in data.Product_Prices
                            where p.PriceID == priceID
                            select p).FirstOrDefault();
                data.Product_Prices.DeleteOnSubmit(price);
                data.SubmitChanges();
            }
        }

        /// <summary>
        /// Update Status of Discount
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newStatus"></param>
        public void UpdateStatusDiscount(int? id, int? newStatus)
        {
            using (var data = new DataDataContext())
            {
                if (id != null && newStatus != null)
                {
                    var dis = (from p in data.Discounts
                                 where p.DiscountsID == id
                                 select p).FirstOrDefault();
                    if (dis != null)
                    {
                        if (dis.Status != newStatus || dis.Status == null)
                            dis.Status = (byte)newStatus;

                        data.SubmitChanges();
                    }
                }

            }
        }

        /// <summary>
        /// Delete Discount
        /// </summary>
        /// <param name="disID"></param>
        public void DeleteDiscount(int disID)
        {
            using (var data = new DataDataContext())
            {
                var dis = (from p in data.Discounts
                             where p.DiscountsID == disID
                             select p).FirstOrDefault();
                data.Discounts.DeleteOnSubmit(dis);
                data.SubmitChanges();
            }
        }


        /// <summary>
        /// Get list Product Stock
        /// </summary>
        /// <param name="id"></param>
        /// <param name="StockInSearch"></param>
        /// <param name="ModifyDateSearch"></param>
        /// <param name="QuantitySearch"></param>
        /// <param name="StatusSearch"></param>
        /// <returns></returns>
        public List<Product_Stock> GetListProductStockByProductID(int id, DateTime? StockInSearch, DateTime? ModifyDateSearch, long? QuantitySearch, int? StatusSearch)
        {
            using (var data = new DataDataContext())
            {
                var proPrice = (from p in data.Product_Stocks
                                where (p.ProductID == id)// && (p.StartDate <= DateTime.Now)
                                select p);


                if (StockInSearch != null && ModifyDateSearch == null)
                {
                    proPrice = proPrice.Where(p => p.StockIn != null && p.StockIn >= StockInSearch);
                }

                if (StockInSearch == null && ModifyDateSearch != null)
                {
                    proPrice = proPrice.Where(p => p.StockIn != null && p.ModifyDate <= ModifyDateSearch);
                }

                if (StockInSearch != null && ModifyDateSearch != null)
                {
                    proPrice = proPrice.Where(p => p.StockIn != null && p.StockIn >= StockInSearch && p.StockIn <= ModifyDateSearch);
                }

                if (QuantitySearch != null)
                {
                    proPrice = proPrice.Where(p => p.Quantity == QuantitySearch);
                }

                if (StatusSearch != null)
                {
                    proPrice = proPrice.Where(p => p.Status == StatusSearch);
                }

                proPrice = (from p in proPrice
                            orderby p.StockIn descending
                            select p);

               
                return proPrice.ToList();
            }
        }

        /// <summary>
        /// Create Stock
        /// </summary>
        /// <param name="sto"></param>
        /// <returns></returns>
        public int CreateStock(Product_Stock sto)
        {
            using (var data = new DataDataContext())
            {
                data.Product_Stocks.InsertOnSubmit(sto);
                data.SubmitChanges();
                return sto.StockID;
            }
        }

        /// <summary>
        /// Get Product Stock by StockID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product_Stock GetProductStockByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var stock = (from p in data.Product_Stocks
                             where p.StockID == id
                             select p).FirstOrDefault();
                return stock;
            }
        }

        /// <summary>
        /// Update Product Stock
        /// </summary>
        /// <param name="sto"></param>
        public void UpdateProductStock(Product_Stock sto)
        {
            using (var data = new DataDataContext())
            {
                var stock = (from p in data.Product_Stocks
                             where p.StockID == sto.StockID
                             select p).FirstOrDefault();
                stock.Quantity = sto.Quantity;
                stock.StockCode = sto.StockCode;
                stock.StockIn = sto.StockIn;
                stock.ModifyDate = sto.ModifyDate;
                stock.NotesStock = sto.NotesStock;
                stock.ProductID = sto.ProductID;
                stock.Status = sto.Status;
                
                data.SubmitChanges();
            }
        }

        /// <summary>
        /// Delete Product in Stock
        /// </summary>
        /// <param name="stockID"></param>
        public void DeleteStock(int stockID)
        {
            using (var data = new DataDataContext())
            {
                var stock = (from p in data.Product_Stocks
                             where p.StockID == stockID
                             select p).FirstOrDefault();
                data.Product_Stocks.DeleteOnSubmit(stock);
                data.SubmitChanges();
            }
        }


        /// <summary>
        /// Update Status for Stock
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newStatus"></param>
        public void UpdateStatusStock(int? id, int? newStatus)
        {
            using (var data = new DataDataContext())
            {
                if (id != null && newStatus != null)
                {
                    var sto = (from p in data.Product_Stocks
                               where p.StockID == id
                               select p).FirstOrDefault();
                    if (sto != null)
                    {
                        if (sto.Status != newStatus || sto.Status == null)
                            sto.Status = (byte)newStatus;

                        data.SubmitChanges();
                    }
                }

            }
        }

        /// <summary>
        /// Create Type Properties
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public int CreateTypeProperties(Type_Property type)
        {
            using (var data = new DataDataContext())
            {
                data.Type_Properties.InsertOnSubmit(type);
                data.SubmitChanges();

                return type.TypePropertiesID;
            }
        }

        /// <summary>
        /// Get TypeProperty
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Type_Property GetTypeProperty(int id)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Type_Properties
                            where p.TypePropertiesID == id
                            select p).FirstOrDefault();
                return type;
            }
        }

        /// <summary>
        /// Update TypeProperty
        /// </summary>
        /// <param name="type"></param>
        public void UpdateTypeProperty(Type_Property type)
        {
            using (var data = new DataDataContext())
            {
                var typeProperty = (from p in data.Type_Properties
                                   where p.TypePropertiesID == type.TypePropertiesID
                                   select p).SingleOrDefault();
                if (type.TypePropertiesName != null)
                {
                    typeProperty.TypePropertiesName = type.TypePropertiesName;
                }

                if (type.TypePropertiesAbstract != null)
                {
                    typeProperty.TypePropertiesAbstract = type.TypePropertiesAbstract;
                }

                if (type.TypePropertiesDes != null)
                {
                    typeProperty.TypePropertiesDes = type.TypePropertiesDes;
                }

                if (type.Status != null)
                {
                    typeProperty.Status = type.Status;
                }

                if (type.LanguagesID != null)
                {
                    typeProperty.LanguagesID = type.LanguagesID;
                }

                data.SubmitChanges();
            }
        }


        /// <summary>
        /// Get List TypeProperty
        /// </summary>
        /// <param name="id"></param>
        /// <param name="languageID"></param>
        /// <param name="Name"></param>
        /// <param name="StatusSearch"></param>
        /// <returns></returns>
        public List<Type_Property> GetListTypeProperty(int? id, int languageID, string Name, int? StatusSearch)
        {
            using (var data = new DataDataContext())
            {
                IQueryable<Type_Property> dis = null;
                if (id != null)
                {
                    dis = (from p in data.Type_Properties
                           where p.TypePropertiesID == id
                           select p);
                }
                else
                {
                    dis = (from p in data.Type_Properties
                           select p);
                }

                if (languageID != null)
                {
                    dis = dis.Where(p => p.LanguagesID == languageID);
                }
                else
                    dis = dis.Where(p => p.LanguagesID == 1);         //1 la vi-VN


                if (Name != string.Empty && Name!=null)
                {

                    dis = dis.Where(p => p.TypePropertiesName == Name);
                }


                if (StatusSearch != null)
                {
                    dis = dis.Where(p => p.Status == StatusSearch);
                }

                //dis = (from p in dis
                //       orderby p.StartDate descending
                //       select p);

                //if (dis.Count() > 0)
                //{
                //    if (dis.FirstOrDefault().EndDate != null)
                //    {
                //        proPrice = (from p in proPrice
                //                    where p.EndDate >= DateTime.Now
                //                    select p);
                //    }
                //}

                return dis.ToList();
            }
        }


        /// <summary>
        /// Get List TypeProperty by Language and SiteGuid
        /// </summary>
        /// <param name="languageID"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>
        public List<Type_Property> GetListTypeProperty(int languageID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                IQueryable<Type_Property> dis = null;

                dis = (from p in data.Type_Properties
                       join swp in data.System_Website_TypeProperties on p.TypePropertiesID equals swp.TypePropertiesID into r0
                       from e0 in r0.DefaultIfEmpty()
                       join sw in data.System_Websites on e0.SiteGuid equals sw.SiteGuid into r1
                       from e1 in r1.DefaultIfEmpty()
                       where p.LanguagesID == languageID && e1.SiteGuid == siteGuid
                       select p);

                return dis.ToList();
            }
        }

        /// <summary>
        /// Update Status of Type Property
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newStatus"></param>
        public void UpdateStatusTypeProperty(int? id, int? newStatus)
        {
            using (var data = new DataDataContext())
            {
                if (id != null && newStatus != null)
                {
                    var dis = (from p in data.Type_Properties
                               where p.TypePropertiesID == id
                               select p).FirstOrDefault();
                    if (dis != null)
                    {
                        if (dis.Status != newStatus || dis.Status == null)
                            dis.Status = (byte)newStatus;

                        data.SubmitChanges();
                    }
                }

            }
        }


        /// <summary>
        /// Get count TypeProperty in ProductTypeProperty
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public int GetCountTypePropertyInProductTypeProperty(int typeId)
        {
            using (var data = new DataDataContext())
            {
                var ProMana = (from p in data.Product_Type_Properties
                               where p.TypePropertiesID == typeId
                               select p);
                return ProMana.Count();
            }
        }

        /// <summary>
        /// Detele typeProperty
        /// </summary>
        /// <param name="typeId"></param>
        public void DeleteTypeProperty(int typeId)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Type_Properties
                            where p.TypePropertiesID == typeId
                            select p).FirstOrDefault();
                data.Type_Properties.DeleteOnSubmit(type);
                data.SubmitChanges();
            }
        }

        /// <summary>
        /// Delete Product Type Properties
        /// </summary>
        /// <param name="id"></param>
        public void DeleteProductTypeProperties(int id)
        {
            using (var data = new DataDataContext())
            {
                var ptp = (from p in data.Product_Type_Properties
                           where p.ProductTypePropertiesID == id
                           select p).FirstOrDefault();
                data.Product_Type_Properties.DeleteOnSubmit(ptp);
                data.SubmitChanges();
            }
        }

        /// <summary>
        /// Get List TypeProperty of Product by ProductID
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public List<Product_Type_Property> GetListProductTypePropertyByProductID(int proId)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Product_Type_Properties
                            where p.ProductID == proId
                            select p);

                return list.ToList();
            }
        }

        /// <summary>
        /// Methoad Search Product basic (Search product in ProductName,ProductCode,ProductDes,Features)
        /// </summary>
        /// <param name="searchText"></param>
        /// <returns></returns>
        public List<Product> GetListProductSearchBasic(string searchText, int languageID, int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                if (searchText == null)
                {
                    return new List<Product>();
                    //var list = (from p in data.Products
                    //            join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                    //            from e0 in r0.DefaultIfEmpty()
                    //            join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                    //            from e1 in r1.DefaultIfEmpty()
                    //            where e1.LanguagesID == languageID && p.Status == 1 && e1.SiteGuid == siteGuid 
                    //            select p).Distinct().ToList();

                    //return list;
                }
                else
                {
                    var list = (from p in data.Products
                                join m in data.Type_Product_Manages on p.ProductID equals m.ProductID into r0
                                from e0 in r0.DefaultIfEmpty()
                                join t in data.Type_Products on e0.TypeProductID equals t.TypeProductID into r1
                                from e1 in r1.DefaultIfEmpty()
                                where e1.LanguagesID == languageID && p.Status == 1 && e1.SiteGuid == siteGuid && (p.ProductName.Contains(searchText) || p.ProductCode.Contains(searchText) || p.ProductDes.Contains(searchText) || p.Features.Contains(searchText))
                                select p).Distinct().ToList();

                    return list;
                }
                
            }
        }


        /// <summary>
        /// Create ProductTypeProperty
        /// </summary>
        /// <param name="ptp"></param>
        /// <returns></returns>
        public int CreateProductTypeProperty(Product_Type_Property ptp)
        {
            using (var data = new DataDataContext())
            {
                data.Product_Type_Properties.InsertOnSubmit(ptp);
                data.SubmitChanges();

                return ptp.ProductTypePropertiesID;
            }
        }
        /// <summary>
        /// Create ProductProperties
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        public int CreateProductProperties(Product_Property pro)
        {
            using (var data = new DataDataContext())
            {
                data.Product_Properties.InsertOnSubmit(pro);
                data.SubmitChanges();

                return pro.PropertiesID;
            }
        }
        /// <summary>
        /// Get Product Properties by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product_Property GetProductPropertiesByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var pro = (from p in data.Product_Properties
                           where p.PropertiesID == id
                           select p).FirstOrDefault();
                return pro;
            }
        }
        /// <summary>
        /// Update ProductProperties
        /// </summary>
        /// <param name="pro"></param>
        public void UpdateProductProperties(Product_Property pro)
        {
            using (var data = new DataDataContext())
            {
                var productProperties = (from p in data.Product_Properties
                               where p.PropertiesID == pro.PropertiesID
                               select p).FirstOrDefault();
                productProperties.ProductTypePropertiesID = pro.ProductTypePropertiesID;
                productProperties.Images = pro.Images;
                productProperties.PropertiesName = pro.PropertiesName;
                productProperties.PropertiesDes = pro.PropertiesDes;
                productProperties.Status = pro.Status;

                data.SubmitChanges();
            }
        }
        /// <summary>
        /// Check TypeProperties for product
        /// </summary>
        /// <param name="proid"></param>
        /// <param name="abtract"></param>
        /// <param name="siteGuid"></param>
        /// <param name="languageID"></param>
        /// <returns></returns>
        public bool CheckTypePropertiesForProductByAbtract(int proid, string abtract,int siteGuid,int languageID)
        {
            using (var data = new DataDataContext())
            {

                var check = (from p in data.Type_Properties
                             join wt in data.System_Website_TypeProperties on p.TypePropertiesID equals wt.TypePropertiesID into r0
                             from e0 in r0.DefaultIfEmpty()
                             where p.TypePropertiesAbstract == abtract && e0.SiteGuid == siteGuid && e0.Status == 1 && p.LanguagesID == languageID
                             select p);
                if (check.Count() > 0)
                    return true;
                else
                    return false;
            }
        }

        public List<System_Website_TypeProperty> GetListSystemWebsiteTypePropertiesBysiteGuid(int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.System_Website_TypeProperties
                            where p.SiteGuid == siteGuid
                            select p).ToList();
                return list;
            }
        }

        public void CreateSystemWebsiteTypeProperties(System_Website_TypeProperty swt)
        {
            using (var data = new DataDataContext())
            {
                data.System_Website_TypeProperties.InsertOnSubmit(swt);
                data.SubmitChanges();
            }
        }

        /// <summary>
        /// Get List Type_Properties for filer control
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="languageID"></param>
        /// <param name="siteGuid"></param>
        /// <returns></returns>
        public List<Type_Property> GetListFilterForTypeProduct(int? typeId, int languageID, int siteGuid)
        {
            using (var data = new DataDataContext())
            { 
                //neu typeId null lay tat ca filer cho website
                List<Type_Property> listFilter=new List<Type_Property>();
                if(typeId!=null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb = GetListTypeProductByID(ref sb, (int)typeId,languageID,siteGuid);
                    var arrId = sb.ToString();
                    listFilter=(from p in data.Type_Properties
                                        join ft in data.Filter_Type_Products on p.TypePropertiesID equals ft.TypePropertiesID 
                                        //from e0 in r0.DefaultIfEmpty()
                                        join tp in data.Type_Products on ft.TypeProductID equals tp.TypeProductID into r1
                                        from e1 in r1.DefaultIfEmpty()
                                        join swt in data.System_Website_TypeProperties on p.TypePropertiesID equals swt.TypePropertiesID into r2
                                        from e2 in r2.DefaultIfEmpty()
                                        where p.LanguagesID==languageID && e2.SiteGuid==siteGuid && (e1.TypeProductID==typeId || arrId.Substring(0,arrId.Length).Split(',').Contains(e1.TypeProductID.ToString()))
                                        select p).ToList();
                }
                else
                {
                    listFilter=(from p in data.Type_Properties
                                        join ft in data.Filter_Type_Products on p.TypePropertiesID equals ft.TypePropertiesID
                                        //from e0 in r0.DefaultIfEmpty()
                                        join tp in data.Type_Products on ft.TypeProductID equals tp.TypeProductID into r1
                                        from e1 in r1.DefaultIfEmpty()
                                        join swt in data.System_Website_TypeProperties on p.TypePropertiesID equals swt.TypePropertiesID into r2
                                        from e2 in r2.DefaultIfEmpty()
                                        where p.LanguagesID==languageID && e2.SiteGuid==siteGuid
                                        select p).ToList();
                }
                
                return listFilter;
            }
        }

        public List<Type_Property> GetListTypePropertiesByParent(int parentId)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Type_Properties
                            where p.ParentType == parentId
                            select p).ToList();
                return list;
            }
        }
        public int CreateProduct_Stock_Detail(Product_Stock_Detail prtProduct_Stock_Detail)
        { 
            using(var data=new DataDataContext())
            {
                data.Product_Stock_Details.InsertOnSubmit(prtProduct_Stock_Detail);
                data.SubmitChanges();
                return prtProduct_Stock_Detail.ProductStockDetailID;
            }

        }
        public int CreateProduct_Stock(Product_Stock prtProduct_Stock)
        {
            using (var data = new DataDataContext())
            {
                data.Product_Stocks.InsertOnSubmit(prtProduct_Stock);
                data.SubmitChanges();
                return prtProduct_Stock.StockID;
            }

        }
        public List<Product_Stock_Detail> getListProductStockDetail(int? id)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.Product_Stock_Details
                            where p.StockID == id
                            select p).DefaultIfEmpty();
                return list.ToList();
            }

        }
        public void DeleteOrderItemDetail(int? ProductStockDetailID)
        {
            using(var data=new DataDataContext())
            {
                var result=(from o_item_Detail in data.Order_Item_Details
                           where o_item_Detail.ProductStockDetailID==ProductStockDetailID
                           select o_item_Detail).FirstOrDefault();
                data.Order_Item_Details.DeleteOnSubmit(result);
                data.SubmitChanges();
            
            }
        }


        #region Edit serial, code
        public int SaveEditProduct_Stock_Detail(Product_Stock_Detail ProductStockDetail)
        {
            using (var data = new DataDataContext())
            {
                var type = (from p in data.Product_Stock_Details
                            where p.ProductStockDetailID == ProductStockDetail.ProductStockDetailID
                            select p).FirstOrDefault();
               
                    type.Code = ProductStockDetail.Code;
                    type.Serial = ProductStockDetail.Serial;
                    type.EndDate = ProductStockDetail.EndDate;
                
                data.SubmitChanges();
                return ProductStockDetail.ProductStockDetailID;
            }
        }
        public void UpdateProduct_Stock_Detail(int id, string Code, string Serial,string endate)
        {
            using (var data = new DataDataContext())
            {
                var type= (from p in data.Product_Stock_Details
                               where p.ProductStockDetailID==id
                               select p).FirstOrDefault();
                if ((!string.IsNullOrEmpty(Code)) && (!string.IsNullOrEmpty(Serial)))
                {
                    type.Code = Code;
                    type.Serial = Serial;
                    type.EndDate = DateTime.Parse(endate);
                }
                data.SubmitChanges();
            }
        }
        #endregion 
    }
}
